<?php

namespace App\Http\Controllers;


use App\Helper\TimeHelper;
use App\Service\FoodService;
use App\Service\UserBuilderService;
use Illuminate\Http\Request;

class MbrFoodController extends Controller
{
    public function index($date, UserBuilderService $userBuilderService)
    {
        //评测
        $builder = $this->bmrUser->lastBuilder()->first();
        if (!$builder) return redirect('/calculate/count');
        $rest = $userBuilderService->getBuilderRestHot($builder, $date);
        $data = [
            'user' => $this->bmrUser,
            'info' => $builder,
            'foods'=> $rest['foods'],
            'date' => $date,
            'totalHot'=> $rest['totalHot'],
            'rest' => [
                'protein'   => $rest['rest']['protein'],
                'fat'       => $rest['rest']['fat'],
                'carbohydrate'=> $rest['rest']['carbohydrate'],
            ]
        ];

        return view('bmr/SearchFood', $data);
    }

    /**查找食物
     * @param Request $request
     * @param FoodService $foodService
     * @return array
     */
    public function search(Request $request, FoodService $foodService)
    {
        $input = $request->all(['category', 'find_extra']);
        return $this->apiSuccess($foodService->findCategory($input['category'], true, isset($input['find_extra']) && $input['find_extra'] == 1 ? true : false));
    }

    /** 用户添加食物
     * @param Request $request
     * @param FoodService $foodService
     */
    public function edit(Request $request, FoodService $foodService, UserBuilderService $userBuilderService)
    {
        $inputs = $request->all(['id', 'weight']);
        $result = $foodService->updateOrCreateUserFood($this->bmrUser, $inputs);
        if ($result === false) {
            return $this->apiError('添加食物失败', -1);
        } else {
            //评测的总营养
            $builder = $this->bmrUser->lastBuilder()->first();
            $rest = $userBuilderService->getBuilderRestHot($builder, TimeHelper::toDay($builder->created_at));
            $result = [
                'data' => array_merge($result,[
                    'food_fat'      => number_format($foodService->getFatHot($result['food_fat']), 2, '.', ''),  //转为热量显示
                    'food_protein'  => number_format($foodService->getProteinHot($result['food_protein']), 2, '.', ''), //转为热量显示
                    'food_carbohydrate'=> number_format($foodService->getCarbohydrateHot($result['food_carbohydrate']), 2, '.', ''), //转为热量显示
                ]),
                'totalHot'=> $rest['totalHot'], //总热量
                'rest' => [ //营养剩余热量
                    'protein'   => $rest['rest']['protein'],
                    'fat'       => $rest['rest']['fat'],
                    'carbohydrate'=> $rest['rest']['carbohydrate'],
                ]
            ];
            return $this->apiSuccess($result);
        }
    }

    /**删除用户食物
     * @param Request $request
     * @param FoodService $foodService
     */
    public function delete(Request $request, FoodService $foodService, UserBuilderService $userBuilderService)
    {
        $inputs = $request->all(['id']);
        if ($foodService->destroyUserFood($this->bmrUser, $inputs)) {
            //评测的总营养
            $builder = $this->bmrUser->lastBuilder()->first();
            $rest = $userBuilderService->getBuilderRestHot($builder, TimeHelper::toDay($builder->created_at));
            return $this->apiSuccess([
                'totalHot'=> $rest['totalHot'], //总热量
                'rest' => [ //营养剩余热量
                    'protein'   => $rest['rest']['protein'],
                    'fat'       => $rest['rest']['fat'],
                    'carbohydrate'=> $rest['rest']['carbohydrate'],
                ]
            ]);
        } else {
            return $this->apiError('删除失败');
        }
    }
}